package cn.jhc.servlet;

import cn.jhc.beans.User;
import cn.jhc.db.SurveyItemDao;
import cn.jhc.db.UserDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Connection connection = (Connection) req.getServletContext().getAttribute("connection");
        User loggedUser = UserDao.login(connection, username, password);
        if (loggedUser != null) {
            req.getSession().setAttribute("loggedUser", loggedUser);
            resp.sendRedirect("index.html");
        } else {
            resp.sendRedirect("login.html");           
        }
    }
}
